package single;

public class SingleMonkeyKing {

    public static void main(String[] args) {
        Guanyin guanyin = new Guanyin();
        guanyin.distinguish();
        Rulai rulai = new Rulai();
        rulai.distinguish();
    }

    public static class MonkeyKing {

        private static MonkeyKing instance;

        private MonkeyKing() {}

        public static synchronized MonkeyKing getInstance() {
            if (instance == null) {
                instance = new MonkeyKing();
            }
            return instance;
        }

        /*public static MonkeyKing getInstance() {
            if (instance == null) {
                instance = new MonkeyKing();
            }
            return instance;
        }*/

        public void eye() {
            System.out.println("我会火眼金睛");
        }

        public void magic() {
            System.out.println("我会72变");
        }

    }

    public static class Guanyin {

        private MonkeyKing monkeyKing1 = MonkeyKing.getInstance();
        private MonkeyKing monkeyKing2 = MonkeyKing.getInstance();

        public void distinguish() {
            System.out.println("猴子1技能");
            monkeyKing1.eye();
            monkeyKing1.magic();
            System.out.println("猴子2技能");
            monkeyKing2.eye();
            monkeyKing2.magic();
        }

    }

    public static class Rulai {

        private MonkeyKing monkeyKing1 = MonkeyKing.getInstance();
        private MonkeyKing monkeyKing2 = MonkeyKing.getInstance();

        public void distinguish() {
            if(monkeyKing1 != monkeyKing2) {
                System.out.println("两个猴子不一样 其中一个必定是假");
            } else {
                System.out.println("是同一个猴子");
            }
        }

    }

}